<template>
  <div id="attraction-detail">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb my-0">
        <li class="breadcrumb-item"><router-link to='/attractions'>景点</router-link></li>
        <li class="breadcrumb-item active" aria-current="page">价格详情</li>
        <span class="ms-auto" @click.stop="back()">返回</span>
      </ol>
    </nav>

    <div class="card"  v-if="detailInfo !== null">
      <table class="table table-striped table-sm">
        <tr>
          <td class="text-info">名称</td>
          <td class="text-info">地址</td>
          <td class=" text-info">开业时间</td>
          <td class=" text-info">状态</td>
          <td class=" text-info">备注</td>
        </tr>
        <tr>
          <td>
            {{detailInfo.name}}
          </td>
          <td>
            {{detailInfo.address}}
          </td>
          <td>
            {{detailInfo.openTime}}
          </td>
          <td>
            {{detailInfo.status}}
          </td>
          <td>
            {{detailInfo.remark}}
          </td>
        </tr>                                  
      </table>
      <div class="card-body my-0 py-0">
        价格详情
        <ul class="nav nav-tabs nav-bordered mb-3" id="myTab" role="tablist">
          <li class="nav-item">
            <a class="nav-link active" id="detail-tab" data-bs-toggle="tab" href="#dailyPrice">每日价格</a>
          </li>
          <li class="nav-item ">
            <a class="nav-link " id="login-tab" data-bs-toggle="tab" href="#newPrice">新建价格</a>
          </li>
        </ul>
        <div class="tab-content" id="myTabContent">
          <div class="tab-pane fade show active" id="dailyPrice">
            <table class="table table-sm">
              <thead>
                <tr>
                  <th>日期</th>
                  <th>价格</th>
                  <th>备注</th>
                  <th></th>
                </tr>
              </thead>
              <tbody>
                <tr v-for="info in prices">
                  <td>{{info.effectDate}}</td>
                  <td>{{info.price}}</td>
                  <td>{{info.remark}}</td>
                  <td>
                    <button class="btn btn-danger btn-sm" @click.stop="deletePrice(info.id)">删除</button>
                  </td>
                </tr>
                <tr>
                   <my-pagination :row-count='sc.rowCount' :page-total='sc.pageTotal' :page-no='sc.pageNo' @next-page='nextPage' @prev-page='prevPage' @direct-page='directPage'></my-pagination>
                </tr>

              </tbody>
            </table>
          </div>
          <div class="tab-pane fade show " id="newPrice">
            <div id="newPrice">
              <div class="row mb-2">
                <label class="control-label col-4 text-end">
                    开始日期
                </label>
                <div class="col-6">
                  <my-date-picker id="beginDate" v-model="beginDate" placeholder="开始日期" sizing=""></my-date-picker>
                </div>
            </div>
            <div class="row mb-2">
                <label class="control-label col-4 text-end">
                    截至日期
                </label>
                <div class="col-6">
                  <my-date-picker id="endDate" v-model="endDate" placeholder="截至日期" sizing=""></my-date-picker>
                </div>
            </div>
            <div class="row mb-2">
                <label class="control-label col-4 text-end">
                    价格
                </label>
                <div class="col-6">
                  <input type="text" class="form-control" v-model.number="price" />
                </div>
            </div>
            <div class="row mb-2">
                <label class="control-label col-4 text-end">
                    备注
                </label>
                <div class="col-6">
                  <input type="text" class="form-control" v-model="remark" />
                </div>
            </div>
            <div class="text-center">
              <button type="button" class="btn btn-primary" @click.stop="savePrice()">保存</button>
            </div>
          </div>
            </div>
        </div>
      </div>
  
         
        </div> 
      </div>
    </div>
  </div>
</template>

<script>
import {
  getAttraction,
  searchAttractionPrices,
  saveAttractionPrice,
  deleteAttractionPrice,
} from "@/api/attraction.js";
import $ from "jquery";
import MyPagination from "@/components/my-pagination.vue";
import MyDatePicker from "@/components/my-datepicker.vue";

export default {
  components: {
    MyPagination,
    MyDatePicker,
  },
  data() {
    return {
      id: 0,

      detailInfo: null,
      prices: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 7,
        pageTotal: 0,
      },

      beginDate: "",
      endDate: "",
      price: 0,
      remark: "",
    };
  },
  computed: {
    idTypes() {
      return this.$store.state.idTypes;
    },
    isAdmin() {
      return this.$store.getters.isDataAdmin;
    },
  },
  mounted: function () {
    this.id = parseInt(this.$route.params.id);
    // console.log(this.id)
    this.search();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      getAttraction(this.id, (v) => {
        this.detailInfo = v;

        this.searchPrices();
      });
    },
    searchPrices: function () {
      const params = {
        pageNo: this.sc.pageNo,
        pageSize: this.sc.pageSize,
      };

      searchAttractionPrices(this.id, params, (v) => {
        this.prices = v.dataList;
        this.sc = v.page;
        console.log(this.page);
      });
    },
    savePrice: function () {
      const params = {
        startDate: this.beginDate,
        endDate: this.endDate,
        price: this.price,
        remark: this.remark,
      };
      saveAttractionPrice(this.id, params, (v) => {
        if (v.status === "OK") {
          this.searchPrices();
          this.showErrMsg("保存成功");
        } else {
          this.showErrMsg(v.errmsg, "danger");
        }
      });
    },
    deletePrice: function (priceId) {
      deleteAttractionPrice(this.id, priceId, (v) => {
        if (v.status === "OK") {
          this.searchPrices();
          this.showErrMsg("删除成功");
        } else {
          this.showErrMsg(v.errmsg, "danger");
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.searchPrices();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.searchPrices();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.searchPrices();
    },
  },
};
</script>